package test;

import java.io.IOException;

import mx.bigdata.jcalais.CalaisClient;
import mx.bigdata.jcalais.CalaisConfig;
import mx.bigdata.jcalais.CalaisObject;
import mx.bigdata.jcalais.CalaisResponse;
import mx.bigdata.jcalais.rest.CalaisRestClient;

public class jCalais {

	public static void main(String[] args) {

		/**
		 * API Key
		 */
		 CalaisClient client = new CalaisRestClient("vrwmtgfzzw4k9e69ncqcaxwh");

		/**
		 *  Configuring response
		 */
		 
		CalaisConfig config = new CalaisConfig();
		config.set(CalaisConfig.ProcessingParam.CALCULATE_RELEVANCE_SCORE,"true");
		config.set(CalaisConfig.ProcessingParam.ENABLE_METADATA_TYPE, "GenericRelations,SocialTags");
		config.set(CalaisConfig.ProcessingParam.CONTENT_TYPE, "TEXT/RAW");
		config.set(CalaisConfig.ProcessingParam.DOC_RDFACCESSIBLE, "true");

		/** 
		 * Calling response
		 */
		
		 CalaisResponse response=null;
		 
		 // Francés:
		 String example = "D\u00E9c\u00E8s d\u2019un des acteurs les plus c\u00E9l\u00E8bres de \u00AB Bollywood \u00BB\n\nLundi 5 d\u00E9cembre 2011\n\nDev Anand, un des acteurs les plus c\u00E9l\u00E8bres de l\u2019\u00E2ge d\u2019or de \u00AB Bollywood \u00BB, l\u2019industrie indienne du cin\u00E9ma , est d\u00E9c\u00E9d\u00E9 \u00E0 Londres \u00E0 l\u2019\u00E2ge de 88 ans, a rapport\u00E9 dimanche l\u2019agence Press Trust of India.\n\nDev Anand en ao\u00FBt 2011 \u00A9 AFP\n\nDev Anand se trouvait dans la capitale britannique pour des examens m\u00E9dicaux et a succomb\u00E9 samedi \u00E0 une attaque, selon PTI qui cite des sources familiales.\n\nSouvent compar\u00E9 en Inde \u00E0 l\u2019acteur am\u00E9ricain Gregory Peck pour son \u00E9l\u00E9gance Dev Anand \u2013 n\u00E9 \u00E0 l\u2019\u00E9poque de l\u2019empire britannique dans ce qui est aujourd\u2019hui le Pakistan \u2013 avait d\u00E9but\u00E9 au cin\u00E9ma en 1946 et avait tourn\u00E9 au total dans plus de 100 films. Il \u00E9tait \u00E9galement producteur et r\u00E9alisateur. \u00BBJe ne peux vivre sans mes films, mon cin\u00E9ma et mon travail. Pour moi, jusqu\u2019\u00E0 ce que je meure, le cin\u00E9ma sera toujours mon premier et mon dernier amour \u00BB, avait-il d\u00E9clar\u00E9 en 2007 au site sp\u00E9cialis\u00E9 bollywoodhungama.com.\n\nIl s\u2019\u00E9tait investi en politique dans les ann\u00E9es 1970, s\u2019opposant notamment \u00E0 la Premi\u00E8re ministre, Indira Gandhi, et lan\u00E7ant un temps son propre parti politique.\n\nL\u2019actuelle g\u00E9n\u00E9ration des \u00E9toiles de \u00AB Bollywood \u00BB multipliait dimanche les hommages en sa m\u00E9moire, et l\u2019\u00E9crivain Salman Rushdie l\u2019a salu\u00E9 sur Twitter: \u00AB Repose en paix Dev Anand. J\u2019ai grandi en regardant tes films. Triste de dire adieu \u00BB.\n\nBollywood est avec Hollywood un des plus grands foyers mondiaux de production cin\u00E9matographique.";
		 
		 // Inglés:
		 //String example = "Karen O and Trent Reznor resume \'Immigrant Song\' for the soundtrack of \'Millennium\'\n\nWednesday, December 14, 2011\n\nFor the soundtrack of the Hollywood version of Millennium: The men who did not like women, the Yeah Yeah Yeahs singer, Trent Reznor (Nine Inch Nails) and Atticus Ross have teamed up for a resumption of \"Immigrant Song \"by Led Zeppelin. The music video directed by David Fincher (also in control of the film) was released December 12.\n\nTrent Reznor and Atticus Ross then work again with David Fincher, after dialing The Social Network Oscar-winning.\n\nThis is not the first time Fincher makes a clip, as he signed \"Express Yourself\" for Madonna and \"Janie\'s Got a Gun\" for Aerosmith.\n\nThe soundtrack is available on iTunes. It includes a cover of \"Is Your Love Strong Enough\" by Bryan Ferry.\n\nThe Girl with the Dragon Tattoo \u00A9 All Rights Reserved\n\nThe film, adapted from the first volume of the trilogy by Stieg Larsson, meets Daniel Craig, Rooney Mara and Christopher Plummer. It will be on screens American and Scandinavian December 21 and will appear in the world in 2012.";
		 
//		 String example = "Prosecutors at the trial of former Liberian President Charles Taylor " 
//		           + " hope the testimony of supermodel Naomi Campbell " 
//		           + " will link Taylor to the trade in illegal conflict diamonds, "
//		           + " which they say he used to fund a bloody civil war in Sierra Leone.";
		try {
			response = client.analyze(example,config);
//			Iterable<CalaisObject> itEnt = response.getEntities();
//			Iterable<CalaisObject> itTop = response.getTopics();
//			Iterable<CalaisObject> itRel = response.getRelations();
//			Iterable<CalaisObject> itSocTag = response.getSocialTags();
//			Iterable<String> itMeta = response.getMeta().getFieldNames();
//			Iterable<String> itInfo =response.getInfo().getFieldNames();

			
			
//			System.out.println("Nombre de los campos de Entidad: "+ itEnt.iterator().next().getFieldNames());
//			 System.out.println("Nombre de los campos de Topic: "+ itTop.iterator().next().getFieldNames());		
//			 System.out.println("Nombre de los campos de Relaciones: "+ itRel.iterator().next().getFieldNames());
//			 System.out.println("Nombre de los campos de Social Tags: "+ itSocTag.iterator().next().getFieldNames());
			// System.out.println("Lo que hay en getMeta: "+ itMeta.iterator().next());
			// System.out.println("Lo que hay en getInfo: "+ itInfo.iterator().next());

//			response = client.analyze("Prosecutors at the trial of former Liberian President Charles Taylor " 
//			           + " hope the testimony of supermodel Naomi Campbell " 
//			           + " will link Taylor to the trade in illegal conflict diamonds, "
//			           + " which they say he used to fund a bloody civil war in Sierra Leone.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 

		/**
		 *  Display recognized tags:
		 */
		
		// Display Entities:
		/**
		 * Possible fields name:
		 * [_typeGroup, _type, name, persontype, nationality, 
		 * commonname, _typeReference, instances, relevance, _uri]
		 */
		for (CalaisObject entity : response.getEntities()) {
		      System.out.println("\n\n"+entity.getField("_type") + ": " 
		                         + entity.getField("name") + ", Relevance: "+ entity.getField("relevance")
		                         + "\n uri: " + entity.getField("_uri"));
		     
		    }
		
		// Display topics:
		
		/**
		 * Possible fields name for Topic:
		 * [_typeGroup, category, classifierName, categoryName, score, _uri]
		 */
		
		System.out.println("\nTopics:");
		for (CalaisObject topic : response.getTopics()) {
		      System.out.println(topic.getField("categoryName")+ ", Score: "+ topic.getField("score")
		    		  +"\nuri: "+topic.getField("_uri"));
		    }
		
		// Display Social Tags:
		
/**
 * Possible field names of Social Tag:
 * [_typeGroup, id, socialTag, name, importance, originalValue, _uri]
 */
		
		for (CalaisObject tag: response.getSocialTags()){
			if(tag.getField("_typeGroup")!= null){
				System.out.println("Social tags: "+tag.getField("_typeGroup"+": " 
						+ "name"));
			}
			
		}
		
		// Display instances:
		
		System.out.println("\nInstances:");
		for (CalaisObject relation : response.getRelations()) {
			System.out.println(relation.getField("_type")+ ", Type Group: "+ relation.getField("_typeGroup"));
		     // System.out.println(relation.getFieldNames());
		    }

	}
}
